1760D - Challenging Valleys - CodeForces Solution


implementation two pointers *1000

Please click on ads to support us..

Python Code:

t=int(input())
result=[]
for i in range(t):
    n=int(input())
    tab=list(map(int,input().split(" ")))
    valleys=0
    if(n==1):
        result.append('YES')
        continue
    l,r=0,0
    while(r<n):
        if((r==n-1)):
            if(l==0):
                valleys+=1
            elif(tab[l-1]>tab[l]):
                valleys+=1
            break
        if((l==0)&(tab[r+1]>tab[r])):
            valleys+=1
        elif(l!=0):
            if((tab[r+1]>tab[r])&((tab[l-1]>tab[l]))):
                valleys+=1
        if(tab[r+1]==tab[r]):
            r+=1
        elif(tab[r+1]!=tab[l]):
            r+=1
            l=r
    if(valleys==1):
        result.append('YES')
    else:
        result.append('NO')

for i in result:
    print(i)


C++ Code:

#pragma GCC optimize("O3")
#include <iostream>
// #include <vector>
// #include <cstring>
// #include <algorithm>
// #include <iomanip>
// #include <math.h>
// #include <iomanip>
// #include <map>
// #include <unordered_map>
// #include <set>
// #include <unordered_set>
// #include <stack>
// #include <queue>
// #include <deque>
using namespace std;

template < typename A, typename B > ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template < typename T_container, typename T = typename enable_if < !is_same<T_container, string>::value, typename T_container::value_type >::type > ostream & operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << '\n'; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define PI 3.14159265358979323846
#define ll long long
#define ld long double
#define fi first
#define se second
#define nl '\n'
#define all(x) x.begin(), x.end()
#define gg(t) cout << "Case #"<< t << ": ";
#define line cout << nl;
#define pline cout << nl << "______________________" << nl;
#define f(i, k, n) for(int i = k; i <= n; i++)
#define ms(x, a, n) f(i, 0, n-1) x[i] = a;

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<ll>
#define vvl vector<vector<ll>>

#define cia(a, n, s) for (int i = s; i < n + s; ++i) cin >> a[i];
#define ciap(a, n, s) for (int i = s; i < n + s; ++i) cin >> a[i].fi >> a[i].se;
#define ciab(a, b, n, s) for (int i = s; i < n + s; ++i) cin >> a[i] >> b[i];
#define cim(a, m, n, s) for (int i = s; i < m + s; ++i) for (int j = s; j < n + s; ++j) cin >> a[i][j];
#define tr(it, a) for(auto &it : a)
#define sz(x) int((x).size())

const ll MOD = 1e9 + 7;
const int INF = 2e9;
const int LIM = 1e6 + 5;

int n, a[LIM], min_num, min_p, tmp, pre;

void run_case()
{
    cin >> n;
    cia(a, n, 0);
    min_num = INF;
    for (int i = 0; i < n; ++i)
    {
        if (a[i] < min_num)
        {
            min_num = a[i];
            min_p = i;
        }
    }
    tmp = min_p;
    pre = min_num;
    while (min_p >= 0)
    {
        if (a[min_p] < pre)
        {
            cout << "NO" << nl;
            return;
        }
        else
        {
            pre = a[min_p];
        }
        min_p--;
    }
    min_p = tmp;
    pre = min_num;
    while (min_p < n)
    {
        if (a[min_p] < pre)
        {
            cout << "NO" << nl;
            return;
        }
        else
            pre = a[min_p];
        min_p++;
    }
    cout << "YES" << nl;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    // freopen("A.INP", "r", stdin);
    // freopen("A.OUT", "w", stdout);
    int t = 1;
    cin >> t;
    f(i, 1, t)
    {
        // gg(i);
        run_case();
    }
}


Comments

Submit
0 Comments
More Questions

236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD